<template>
  <div class="home-page">
    <div class="home-title-view">
      <div class="home-time">
        <span class="time-text">{{ dateTime }}</span>
        <span class="week-text">{{ dateToWeek(dateText) }}</span>
        <span class="date-text">{{ dateText }}</span>
      </div>
      <!-- <div class="btn-right" @click="btnExit">
        <img src="@/assets/dataStation/eixt.png">
        控制台</div> -->
      <a class="btn-right" href="javascript:window.opener=null;window.close()">
        <img src="@/assets/dataStation/eixt.png">
        控制台
      </a>
      <div class="gif-title-bg">
        <!-- <div class="farmName-view">{{ farmName }}大数据平台</div> -->
      </div>
    </div>
    <div class="content-view">
      <div
        style="width:50%;
            margin: 0px auto;
            position: absolute;
            z-index: 999;
            top: 156px;
            left:50%;
            margin-left:-25%;
            height: 650px"
      >
        <div style="position:relative;height:650px;width:100%;">
          <!--<landView :city-name="cityName" :map-list="mapList" />-->
        </div>
      </div>
      <div class="content-left">
        <div class="min-title-left" />
        <el-date-picker
          v-model="yearTime"
          :clearable="false"
          class="date-picker-view"
          popper-class="popper-date-picker-view"
          style="width:100px;position: absolute;right: 60px;z-index: 999;"
          type="year"
          value-format="YYYY"
          format="YYYY"
          placeholder="选择年"
          @change="changeTime"
        />
        <div class="mubiao-view">
          <div class="mb-left">
            <img src="@/assets/dataStation/mb.gif">
            <div>
              <div class="mb-title">目标产量(kg)</div>
              <div class="mb-num">{{ targetYield }}</div>
            </div>
          </div>
          <div class="mb-right">
            <img src="@/assets/dataStation/sj.gif">
            <div>
              <div class="mb-title">实际产量(kg)</div>
              <div class="mb-num">{{ yieldTotal }}</div>
            </div>
          </div>
        </div>
        <template v-for="(item,index) in breedList">
          <breed max-width="70%" :title-type="index+1" :obj-data="item" />
        </template>
        <el-empty v-if="breedList.length==0" class="empty-view" style="width: 70%;" :image="emptImg" :image-size="100" />
        <div class="max-land-center">
          <div class="min-land-center" />
        </div>
      </div>
      <div class="content-center" style="position: relative;">
        <div class="header-grid">
          <div class="header-select-item" style="width:23%">
            <el-select v-model="cityName" popper-class="select-popper-address" placeholder="请选择" @change="changeCity">
              <el-option
                v-for="item in areaLists.filter((item)=>{return item.name !='香港特别行政区'&&item.name !='澳门特别行政区'})"
                :key="item.code"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </div>
          <div class="header-grid-item" style="width:23%">

            <div v-if="sumFarm>=10000" class="name">{{ Number((sumFarm/10000).toFixed(2)) }}<span>万</span></div>
            <div v-else class="name">{{ sumFarm }}</div>
            <div class="title">种植户</div>
          </div>
          <div class="header-grid-item" style="width:23%">
            <div v-if="mapAreaSize>=10000&&mapAreaSize<100000000" class="name">{{ Number((mapAreaSize/10000).toFixed(2)) }}<span>万</span></div>
            <div v-if="mapAreaSize>=100000000" class="name">{{ Number((mapAreaSize/100000000).toFixed(2)) }}<span>亿</span></div>
            <div v-else class="name">{{ mapAreaSize }}</div>
            <div class="title">耕地面积(亩)</div>
          </div>
          <div class="header-grid-item" style="width:23%">
            <div v-if="useMapAreaSize>=10000&&useMapAreaSize<100000000" class="name">{{ Number((useMapAreaSize/10000).toFixed(2)) }}<span>万</span></div>
            <div v-if="useMapAreaSize>=100000000" class="name">{{ Number((useMapAreaSize/100000000).toFixed(2)) }}<span>亿</span></div>
            <div v-else class="name">{{ useMapAreaSize }}</div>
            <div class="title">种植面积(亩)</div>
          </div>
        </div>
        <div v-if="mapList.length" class="left-bottom-box">
          <div v-for="(item,index) in mapBottomList" :key="index" class="left-bottom-view">
            <div class="item-text"><i :style="{backgroundColor:item.color}" /> {{ item.cropName }}<span>{{ item.mapAreaSize }}亩</span></div>
            <!-- <div class="item-text"><i style="background-color:#FB9E32" />玉米<span>9000亩</span></div>
            <div class="item-text"><i style="background-color:#0480FF" />高粱<span>139000亩</span></div>
            <div class="item-text"><i style="background-color:#D0CE14" />油菜<span>9000亩</span></div> -->
          </div>
        </div>
      </div>
      <div class="content-right">
        <div class="min-title-right" />
        <el-date-picker
          v-model="yearTimeTopRight"
          popper-class="popper-date-picker-view"
          :clearable="false"
          class="date-picker-view"
          style="width:100px;position: absolute;right: 20px;z-index: 999;"
          value-format="YYYY"
          format="YYYY"
          type="year"
          placeholder="选择年"
        />
        <div class="max-chart-view">
          <div class="chart-view">
            <chartRanking :city-name="cityName" :year="yearTimeTopRight" />
          </div>
        </div>
        <div class="max-chart-view">
          <div
            style="width:70%;display: flex;margin-top:5px;justify-content: center;"
          > <div class="min-title-next">
            <el-date-picker
              v-model="yearTimeNextRight"
              style="width:100px;"
              :clearable="false"
              popper-class="popper-date-picker-view"
              class="date-picker-view"
              value-format="YYYY"
              format="YYYY"
              type="year"
              placeholder="选择年"
            />
          </div>
          </div>
        </div>
        <plantingYield :city-name="cityName" :year="yearTimeNextRight" />
        <div class="max-land-right">
          <div class="min-land-right" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">

import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import breed from './components/breed.vue'
import landView from './components/land.vue'
import chartRanking from './components/chartRanking.vue'
import plantingYield from './components/plantingYield.vue'
import { dateToWeek, formatDate } from '@/utils/format'
import { getBenefits, getMapList } from '@/api/board'
import areaLists from '@/api/area'
import emptImg from '@/assets/dataStation/kong.png'
import moment from 'moment'
const router = useRouter()
import { useUserStore } from '@/pinia/modules/user'
const userStore = useUserStore()

const yearTime = ref('2023')
const yearTimeTopRight = ref('2023')
const yearTimeNextRight = ref('2023')
const dateText = ref('--')
const dateTime = ref('--')
const targetYield = ref('0')
const yieldTotal = ref('0')

const mapAreaSize = ref('--')
const useMapAreaSize = ref('--')
const sumFarm = ref('--')
const farmName = ref('--')
const cityName = ref('重庆市')
const breedList = ref([])
const mapList = ref([])
const mapBottomList = ref([])

const btnExit = () => {
  router.go(-1)
}
const currentDate = new Date()
dateText.value = formatDate(currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate())

const getTime = () => {
  setInterval(() => {
    const currentTime = new Date()
    dateTime.value = moment(currentTime).format('HH:mm:ss')
  }, 1000)
}
const getRandomColor = (index) => {
  const colors = ['#00E8A5', '#FB9E32', '#0480FF', '#D0CE14'] // 可选的颜色数组
  return colors[index] || colors[2] // 返回随机选择的颜色
}

// 使用reduce()方法，将每个对象的值依次累加
const sumList = (arr) => {
  let total = 0
  arr.forEach(element => {
    total += (element.mapAreaSize || 0)
  })
  return Number(total.toFixed(2))
}

const bulletinTotal = async() => {
  const res = await getBenefits({
    province: cityName.value === '全国' ? '' : cityName.value,
    year: yearTime.value
  })
  if (res.code === 0) {
    yieldTotal.value = res.data?.yield || '0'
    targetYield.value = res.data?.targetYield || '0'
    breedList.value = res.data?.lineRspList || []
  }
  const resmap = await getMapList({
    province: cityName.value === '全国' ? '' : cityName.value,
    year: yearTime.value
  })
  if (resmap.code === 0) {
    //
    mapAreaSize.value = resmap.data?.mapAreaSize || '0'
    useMapAreaSize.value = resmap.data?.useMapAreaSize || '0'
    sumFarm.value = resmap.data?.sumFarm || '0'
    const newMaplist = resmap.data?.lineRspList || []

    let unipList = []
    newMaplist.forEach(list => {
      unipList.push(list.cropName)
    })
    unipList = Array.from(new Set(unipList))
    mapBottomList.value = []
    unipList.forEach((item, index) => {
      mapBottomList.value.push({
        cropName: item,
        color: getRandomColor(index),
        mapAreaSize: sumList(newMaplist.filter((l) => {
          return l.cropName === item
        }))
      })
    })

    mapList.value = newMaplist.map((item) => {
      item.color = mapBottomList.value.filter((list) => {
        return list.cropName === item.cropName
      })[0].color
      return item
    })
  }
}
onMounted(() => {
  const fList = userStore.userInfo.farmList.filter((item) => {
    return item.farmId === userStore.userInfo.farmId
  })
  if (fList.length > 0) {
    farmName.value = fList[0].farmName
  }
  getTime()
  bulletinTotal()
})

const changeCity = (value) => {
  cityName.value = value
  bulletinTotal()
}
const changeTime = (value) => {
  bulletinTotal()
}
</script>
<style>
  .header-select-item  .el-select .el-input__inner{
      background-color:transparent;
      border: none;
      background-image: url('@/assets/dataStation/select-bg.png');
      background-size: 100% 100%;
      background-repeat: no-repeat;
      background-position: bottom;
      color:#A7FFEB;
      position: relative;
      padding-left:60px;
      padding-right:54px;
      font-size:16px;
      overflow: hidden;
  }
  .header-select-item  .select-trigger{
    position:relative;
  }
  .header-select-item  .select-trigger::after{
      background-image: url('@/assets/dataStation/address-select.png');
      background-size: cover;
      background-repeat: no-repeat;
      background-position: center;
      content: '';
      height: 30px;
      width: 30px;
      position: absolute;
      left: 10px;
      top: 50%;
      margin-top: -15px;
  }
  .header-select-item .el-select .el-input .el-select__caret.el-icon{
    color:#A7FFEB;
    font-weight:bold;
    font-size:14px;
  }
  .header-select-item .el-input__suffix{
    right:30px;
  }
  .date-picker-view .el-input__inner{
    background-color:transparent;
    color:#A7FFEB;
    border:none;
    padding-right: 10px;
    padding-left: 40px;
  }
  .date-picker-view  .el-input__icon.el-icon{
    color:#A7FFEB;
  }
  .date-picker-view .el-input__prefix{
    left:80px;
  }
  .empty-view .el-empty__description p{
    color:#A4D9CD;
    font-size:14px;
  }
</style>
<style scoped lang="scss">
.home-page {
   background-image: url('@/assets/dataStation/bg.png');
   background-size: cover;
   background-repeat: no-repeat;
   background-position: bottom;
     min-width: 1920px;
    min-height: 1080px;
    margin: 0 auto;
    padding: 0;
   .date-picker-view{
    position:absolute;
    right:15%;
   }
   .home-title-view{
      background-image: url('@/assets/dataStation/title-bg.png');
      background-size: cover;
      background-repeat: no-repeat;
      background-position: center;
      height:100px;
      width:100%;
      position: relative;
      .home-time{
        padding: 15px 16px;
        .time-text{
            color:#CCEFE6;
            font-size:25px;
            text-shadow: 0px 0px 10px rgba(0,232,165,0.55);
          }
          .date-text,
          .week-text{
            color:#79E1D0;
            font-size:14px;
          }
          .week-text{
            padding:0 10px;
          }
      }
      .btn-right{
        background-image: url('@/assets/dataStation/btn-right-top.png');
        background-size: cover;
        background-repeat: no-repeat;
        background-position: center;
        position: absolute;
        width:80px;
        right: 25px;
        top: 18px;
        text-align: center;
        font-size: 13px;
        color: #2998E1;
        line-height: 32px;
        color: #CCEFE6;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: bold;
         cursor: pointer;
         z-index:99;
        img{
          height:20px;

        }
      }
      .gif-title-bg{
        background-image: url('@/assets/dataStation/header2-bg.gif');
        background-size: cover;
        background-repeat: no-repeat;
        background-position: center;
        height: 100px;
        width: 100%;
        position:absolute;
        top:0;
        z-index:9;
        .farmName-view{
        margin:0 auto;
        text-align:center;
        font-size:40px;
        color:#fff;
        line-height:70px;
        background: linear-gradient(360deg, #7ACBB9 0%, #FFFDFD 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        font-weight:bold;
      }
    }
   }
   .content-view{
    height:calc(100vh - 100px);
    width:100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position:relative;
    min-height:976px;
    .header-grid{
      display: flex;
      align-items: center;
      justify-content:space-between;
      padding-top:30px;
      .header-select-item{
        .el-select .el-input__inner{
          background-color:transparent;
        }
      }
      .header-grid-item{
        background: linear-gradient(180deg, #00727920 0%, #00757C20 100%);
        text-align:center;
        padding:20px 0;
        background-image: url('@/assets/dataStation/c-title-bg.png');
        background-size: 100% 100%;
        background-repeat: no-repeat;
        background-position: center;
        .name{
          font-size: 32px;
          color:#B1FFED;
          font-weight:600;
          span{
            font-size: 16px;
            margin-left:4px;
            font-weight:400;
          }
        }
        .title{
          font-size: 16px;
          color:#B1FFED;
          padding-top:10px;
        }
      }
    }
    .left-bottom-box{
      position:absolute;
      bottom:100px;
      left:0;
      display:flex;
      justify-content: center;
      width: 100%;
      flex-wrap: wrap;
    .left-bottom-view{
        background-image: url('@/assets/dataStation/left-bottom-bg.png');
        background-size:100% 100%;
        background-repeat: no-repeat;
        background-position: top;
        max-height:100px;
        padding:12px;
        margin-right:10px;
        margin-bottom:10px;
        .item-text{
          font-size:12px;
          color:#7ACBB9;
          position:relative;
          display:flex;
          align-items:center;
          min-width:120px;
          i{
            display:block;
            height:10px;
            width:10px;
            background-color:#00E8A5;
            margin-right:6px;
            border-radius:2px;
          }
          span{
            margin-left:10px;
          }
        }
      }
    }
   .content-right,
   .content-left{
      width: 30%;
      height:calc(100vh - 100px);
      min-height:976px;
      min-width:582px;
      background-image: url('@/assets/dataStation/left-bg.png');
      background-size:100% 100%;
      background-repeat: no-repeat;
      background-position: top;
      position:relative;
      padding-left: 22px;
      .min-title-right,
      .min-title-left{
        background-image: url('@/assets/dataStation/min-title-left.png');
        background-size:cover;
        background-repeat: no-repeat;
        background-position: center;
        height:48px;
        width:80%;
        max-width: 374px;
        position: absolute;
        top: -2px;
        left: 50%;
        margin-left: -187px;
        display: flex;
        align-items: center;
        justify-content: end;
        z-index:99;
      }
      .min-title-right{
        background-image: url('@/assets/dataStation/min-title-right.png');
      }
      .min-title-next{
        background-image: url('@/assets/dataStation/ptop6.png');
        background-size:cover;
        background-repeat: no-repeat;
        background-position: center;
        height:56px;
        width:100%;
        position:relative;
        display: flex;
        align-items: center;
        justify-content: end;
        z-index:99;
      }
      .mubiao-view{
          display:flex;
          justify-content:left;
          padding-top: 64px;
          width:80%;
          .mb-right,
          .mb-left{
            display:flex;
            justify-content:center;
            color:#A4D9CD;
            background-image: url('@/assets/dataStation/mb-bg.png');
            background-size:100% 100%;
            background-repeat: no-repeat;
            background-position: center;
            align-items: center;
            height:122px;
            width:46%;
            .mb-title{
              font-size:16px;
            }
            .mb-num{
              font-size:16px;
              padding-top:6px;
              font-weight: 600;
              color: #B7FFEE;
            }
            img{
              height: 70%;
              margin-right: 10px;
              margin-top:10px;
            }
          }
          .mb-left{
              margin-right:6px;
            }
      }
      .max-land-center{
        background-image: url('@/assets/dataStation/right-qq.png');
        background-size: cover;
        background-repeat: no-repeat;
        background-position: 0px 55px;
        width: 180px;
        min-height: 976px;
        position: absolute;
        z-index:9;
        right: -25px;
        top: 0;
      .min-land-center{
        background-image: url('@/assets/dataStation/right-qq.gif');
        background-size: auto 80%;
        background-repeat: no-repeat;
        background-position: -40px 50%;
        width: 150px;
        height: calc(100vh - 100px);
        position: absolute;
        margin-left: 50px;
        top: 0;
        min-height: 976px;
      }
    }
   }
   .content-right{
    background-image: url('@/assets/dataStation/right-bg.png');
    padding-right:16px;
    .max-chart-view{
      display: flex;
      justify-content: end;
      flex-wrap: wrap;
      .chart-view{
        width:75%;
        height:30vh;
        min-height:300px;
        position:relative;
      }
    }
    .max-land-right{
        background-image: url('@/assets/dataStation/left-qq.png');
        background-size: cover;
        background-repeat: no-repeat;
        background-position: right 45px;
        width: 180px;
        min-height: 976px;
        position: absolute;
        z-index: 9;
        left: -25px;
        top: 0;
      .min-land-right{
        background-image: url('@/assets/dataStation/left-qq.gif');
        // background-size: cover;
        // background-repeat: no-repeat;
        // background-position: right 0px;
        // width: 130px;
        background-size: auto 80%;
        background-repeat: no-repeat;
        background-position: right 48%;
        width: 180px;
        height: calc(100vh - 100px);
        position: absolute;
        left: -10px;
        top: 0;
        min-height: 976px;
      }
    }
   }
   .content-center{
    width:40%;
    height:calc(100vh - 100px);
    min-height:976px;
   }
 }
}
</style>
